import Vue from 'vue'
import { ACCESS_TOKEN } from "@/vuex/mutation-types"

const user = {
  state: {
    token: '',
    openId: '',
    userId: '',
    userName: '',
    realName: '',
    work_no: '',
    appId: '',
    fdeptid: '',
    fdeptidName: ''
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USERINFO: (state, { openId, userId, username, realname, work_no, appId, fdeptid, fdeptidName }) => {
      state.openId = openId;
      state.userId = userId;
      state.username = username;
      state.realname = realname;
      state.work_no = work_no;
      state.appId = appId
      state.fdeptid = fdeptid
      state.fdeptidName = fdeptidName
    }
  },
  actions: {
    setUserInfo({ commit }, userInfo) {
      console.log(userInfo)
      return new Promise(() => {
        commit('SET_USERINFO', { openId: userInfo.openId, userId: userInfo.userId, username: userInfo.username, realname: userInfo.realname, token: userInfo.token, work_no:userInfo.work_no, appId: userInfo.appId, fdeptid: userInfo.fdeptid,
          fdeptidName: userInfo.fdeptidName })
        Vue.ls.set(ACCESS_TOKEN, userInfo.token, 7 * 24 * 60 * 60 * 1000)
        commit('SET_TOKEN', userInfo.token)
      })
    },
    reLogin() {
      return new Promise(() => {
        Vue.ls.remove(ACCESS_TOKEN)
      })
    }
  }
}

export default user
